import { type Model } from 'mongoose';
import { CreatePermActionDto, PermAction, PermActionDocument, UpdatePermActionDto } from '@ai/entity';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { arrToTree } from '@/utils';

@Injectable()
export class PermActionService implements OnModuleInit {
    constructor(@InjectModel(PermAction.name) private readonly permActionModel: Model<PermActionDocument>) {}

    onModuleInit() {
        this.permActionModel.init();
    }
    create(createPermActionDto: CreatePermActionDto) {
        return this.permActionModel.create(createPermActionDto);
    }

    async findAll() {
        return arrToTree(await this.permActionModel.find().exec(), 'parent');
    }

    findOne(id: string) {
        return this.permActionModel.findById(id).exec();
    }

    update(id: string, updatePermActionDto: UpdatePermActionDto) {
        return this.permActionModel.findByIdAndUpdate(id, updatePermActionDto);
    }

    remove(id: string) {
        return this.permActionModel.findByIdAndDelete(id);
    }
}
